﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PerfAssist.Common.Editor
{
    public class PAChartPlotPoint
    {
        public int m_frameID;
        public float m_value;

        public PAChartPlotPoint(int frame, float val)
        {
            m_frameID = frame;
            m_value = val;
        }
    }

    public class PAChartDataSet : IEnumerable
    {
        private readonly Queue<PAChartPlotPoint> m_dataSet;

        public PAChartDataSet(int capacity)
        {
            m_dataSet = new Queue<PAChartPlotPoint>(capacity);
            AutoShrinkMaxSize = capacity;
        }

        public int Count
        {
            get { return m_dataSet.Count; }
        }

        public int AutoShrinkMaxSize { get; set; }

        public IEnumerator GetEnumerator()
        {
            return m_dataSet.GetEnumerator();
        }

        public void Append(PAChartPlotPoint pt)
        {
            m_dataSet.Enqueue(pt);

            if (AutoShrinkMaxSize > 0 &&
                AutoShrinkMaxSize < m_dataSet.Count)
                ShrinkTo(AutoShrinkMaxSize);
        }

        public void ShrinkTo(int newSize)
        {
            while (newSize < m_dataSet.Count) m_dataSet.Dequeue();
        }

        public void GetStatistics(out float maxVal, out float minVal, out float avgVal)
        {
            maxVal = 0.0f;
            minVal = 0.0f;
            avgVal = 0.0f;

            if (m_dataSet.Count == 0)
                return;

            var sum = 0.0f;
            foreach (var item in m_dataSet)
            {
                maxVal = Mathf.Max(maxVal, item.m_value);
                minVal = Mathf.Min(minVal, item.m_value);
                sum += item.m_value;
            }

            avgVal = sum / m_dataSet.Count;
        }
    }
}